package com.liu.stockmarket.repository.database

import com.liu.stockmarket.repository.database.dao.ProfitDao
import com.liu.stockmarket.repository.database.entity.ProfitEntity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.time.LocalDate

class ProfitRepository(private val profitDao: ProfitDao) {
    suspend fun save(profitEntity: ProfitEntity) = profitDao.save(profitEntity)
    suspend fun save(profitEntitys: List<ProfitEntity>, date: LocalDate):Boolean {
        return withContext(Dispatchers.IO){
            profitDao.deleteByDate(date)
            profitDao.save(profitEntitys)
            true
        }

    }


    suspend fun queryAll() = profitDao.queryAll()

    suspend fun deleteAll() = profitDao.deleteAll()

    suspend fun deleteAllForSaveAll(profitEntitys: List<ProfitEntity>) {
        profitDao.deleteAll()
        profitDao.save(profitEntitys)
    }
}